﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HAP = HtmlAgilityPack;

namespace StockImport.dataimport.tse.dailyindex
{
    public class StockDailyIndexParser
    {
        public static List<List<String>> processDailyTradeDataByHtml(String content, ref DateTime dataTime)
        {
            HAP.HtmlDocument doc = new HAP.HtmlDocument();
            doc.LoadHtml(content);
            HAP.HtmlNode input_date = doc.DocumentNode.SelectSingleNode("//input[@name='input_date']");
            if (input_date == null)
            {
                return null;
            }

            String ds = input_date.GetAttributeValue("value", "");
            DateTime rd = DateTime.Parse(ds);
            rd = rd.AddYears(1911);
            dataTime = rd;

            HAP.HtmlNode div = doc.DocumentNode.SelectSingleNode("//div[@id='tbl-containerx']");
            if (div == null)
            {
                return null;
            }

            HAP.HtmlNodeCollection rows = div.SelectNodes(div.XPath + "/table[1]/tbody[1]/tr[@class='basic2']");
            if (rows == null)
            {
                return null;
            }
            List<List<String>> allrows = new List<List<string>>(900);
            foreach (HAP.HtmlNode row in rows)
            {
                String p = row.XPath;
                HAP.HtmlNodeCollection tds = row.SelectNodes(p + "/td");

                List<String> rowData = new List<string>(30);
                foreach (HAP.HtmlNode n in tds)
                {
                    String txt = n.InnerText;
                    txt = txt.Replace("&nbsp;", "").Trim();
                    rowData.Add(txt);
                }
                allrows.Add(rowData);
            }

            return allrows;

        }


    }
}
